package com.huika.renrench.control.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseFra;
import com.huika.renrench.control.investment.activity.BookInvestActivity;
import com.huika.renrench.control.investment.activity.ImmediatelyInvestActivity;
import com.huika.renrench.control.my.activity.MakeOverActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.MyInvestmentbean;
import com.huika.renrench.utils.WebNetUtils;
import com.huika.renrench.view.webview.BaseWebViewAct;

/**
 * @author yangyuan
 * @date：2015年6月3日
 * @describe:我的投资
 */
public class MyInvestmentFra extends BaseFra{
	
	private ImageView btn_policy_statement;//投资说明
	private TextView text_earnings;//收益
	private TextView text_submission;//在投金额
	private Button button_make_over;//债权转让
	private TextView text_accumulated_income;//累计收益
	private Button button_newly_increased;//新增投资
	
	////////////增加 体验金和 体验天数   ( 2015-10-23 by wuwenfeng ) //////////////
	private TextView text_accumulated_experience;//体验金
	private TextView text_accumulated_experience_day;//体验天数
	
	private MyInvestmentbean bean;

	@Override
	protected View inflaterView(LayoutInflater inflater, ViewGroup container,
			Bundle bundle) {
		return inflater.inflate(R.layout.my_investment_fra, container, false);
	}
	
	@Override
	protected void initWidget(View parentView) {
		super.initWidget(parentView);
		
		btn_policy_statement = (ImageView) parentView.findViewById(R.id.btn_policy_statement);
		text_earnings = (TextView) parentView.findViewById(R.id.text_earnings);
		text_submission = (TextView) parentView.findViewById(R.id.text_submission);
		button_make_over = (Button) parentView.findViewById(R.id.button_make_over);
		text_accumulated_income = (TextView) parentView.findViewById(R.id.text_accumulated_income);
		text_accumulated_experience = (TextView) parentView.findViewById(R.id.text_accumulated_experience);
		text_accumulated_experience_day = (TextView) parentView.findViewById(R.id.text_accumulated_experience_day);
		button_newly_increased = (Button) parentView.findViewById(R.id.button_newly_increased);
		
		showLoadingDialog(" ");
		setOclicklisen();
	}
	
	private void setOclicklisen() {
		btn_policy_statement.setOnClickListener(this);
		button_make_over.setOnClickListener(this);
		button_newly_increased.setOnClickListener(this);
	}

	
	@Override
	protected void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.btn_policy_statement:
			Intent intent = new Intent(getActivity(), BaseWebViewAct.class);
			intent.putExtra(BaseWebViewAct.PAGE_URL, WebNetUtils.getWebUrl("10"));
			intent.putExtra(BaseWebViewAct.PAGE_TITLE, "投资细则");
			intent.putExtra(BaseWebViewAct.IS_SHOW_BOTTOM, false);
			intent.putExtra(BaseWebViewAct.IS_HELP_FEEDBACK, true);
			showActivity(getActivity(), intent);
			break;  
		case R.id.button_make_over://债权转让
			if (bean==null) {
				showToastMsg("数据错误"); 
				return;
			}
			Bundle bundle = new Bundle();
			bundle.putString(MakeOverActivity.CANINVESTAMOUNT, bean.getCanTransferBalanc());
			bundle.putString(MakeOverActivity.INVESTAMOUNT, bean.getInvestAmount());
			bundle.putString(MakeOverActivity.MESSAGESTR, bean.getMessageStr());
			bundle.putDouble(MakeOverActivity.AMOUNTM2, bean.getAmountM2());
			bundle.putDouble(MakeOverActivity.AMOUNTM3, bean.getAmountM3());
			bundle.putDouble(MakeOverActivity.RATE2, bean.getRate2());
			bundle.putDouble(MakeOverActivity.RATE3, bean.getRate3());
			showActivity(getActivity(), MakeOverActivity.class,bundle);
			break;
		case R.id.button_newly_increased://新增投资
			if (bean==null) {
				showToastMsg("数据错误"); 
				return;
			}
			Intent intentCan = null;
			if( bean.getCanInvestBalanc()>0) {
			    intentCan = new Intent(getActivity(), ImmediatelyInvestActivity.class);
				intentCan.putExtra("investBalance", bean.getCanInvestBalanc()+"");
			} else if(bean.getCanInvestBalanc() == 0) {
				intentCan = new Intent(getActivity(), BookInvestActivity.class);
				intentCan.putExtra("flag", 1);
			}
			showActivity(getActivity(), intentCan);
			break;
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//调网络，保证数据实时更新
		executeMyInvestmentInfoList();
	}
	
	private void executeMyInvestmentInfoList(){
		FormResultRequest<MyInvestmentbean> request = new FormResultRequest<MyInvestmentbean>(UrlConstants.GETMYINVEST, getMyInvestmentResp, this, new TypeToken<RequestResult<MyInvestmentbean>>() {}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();//
		ajaxParams.putCommonTypeParam("userId", GlobalApp.getInstance().getUserId());//userId
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private Listener<RequestResult<MyInvestmentbean>> getMyInvestmentResp=new Listener<RequestResult<MyInvestmentbean>>() {
		
		@Override
		public void onResponse(RequestResult<MyInvestmentbean> result) { 
			
			if (result.flag == 1) {
				setView(result.getRs());
			} else {
				showToastMsg(result.msg);
			}
			dismissLoadingDialog();
			if (mOverlayLayout != null)
				hideOverLay();
		}
	};
	
	private void setView(MyInvestmentbean bean){
		if (bean==null) {
			showToastMsg("下载错误");
			return;
		}
		this.bean = bean;
		text_earnings.setText(MoneyShowTool.twolastDFStr(bean.getDayAmount())); 
		text_submission.setText(MoneyShowTool.twolastDFStr(bean.getInvestAmount()));
		text_accumulated_income.setText(MoneyShowTool.twolastDFStr(bean.getRevenueAmount()));
		text_accumulated_experience.setText(MoneyShowTool.twolastDF(bean.getExperienceAmount()));
		
		text_accumulated_experience_day.setVisibility(bean.getExperienceDay() > 0 ? View.VISIBLE : View.INVISIBLE);
		text_accumulated_experience_day.setText("体验金不可提现、投资，可享受" + bean.getExperienceDay() + "天的收益，收益可提现。");
	}
	
}
